#include <buola/media/video/bvideotestsrc.h>

#include <buola/image/cimagegc.h>

namespace buola { namespace blackbox {

BVideoTestSrc::BVideoTestSrc()
    :   BBox("video test source")
    ,   mSize(640,480)
    ,   mPos(200,200)
    ,   mDirection(5*sqrt(2),5*sqrt(2))
    ,   oImage(this,"image out")
    ,   kOut(this,"clock out")
{
}

void BVideoTestSrc::CreateImage()
{
    img::CImage_rgb8 lImage(mSize,img::CColor(L"lightcoral"));
    
    img::CImageGC<img::TFormat_rgb8> lGC(lImage);

    lGC.SetLineWidth(3.5);
    lGC.SetLineJoin(img::ELineJoin::MITER);

    CPoint_d lPos(round(mPos.x),round(mPos.y));
    
    lGC.Path().MoveTo(lPos);
    lGC.Path().CurveTo(lPos+CPoint_d(166,0),lPos+CPoint_d(83,83),lPos+CPoint_d(166,166));
    lGC.Path().CurveTo(lPos+CPoint_d(0,166),lPos+CPoint_d(83,83),lPos);
    
    lGC.SetSource(img::CColor(L"HotPink"));
    lGC.FillPath(true);
    lGC.SetSource(img::CColor(L"DeepPink",0.5));
    lGC.StrokePath();

    oImage=std::move(lImage);
    kOut.Trigger();
    
    mPos+=mDirection;
    
    if(mPos.x>mSize.x-166||mPos.x<0)
    {
        mDirection.x=-mDirection.x-0.05+random::get<double>()*0.1;
        mDirection/=norm(mDirection)/10;
    }
    if(mPos.y>mSize.y-166||mPos.y<0)
    {
        mDirection.y=-mDirection.y-0.05+random::get<double>()*0.1;
        mDirection/=norm(mDirection)/10;
    }
}

/*namespace blackbox*/ } /*namespace buola*/ }
